//index.js
//获取应用实例
var Bmob = require('../../utils/bmob.js');
var common = require('../../utils/common.js');
var app = getApp();
var that;
Page({
  
  data: {
    username: null,
    deals: [],
    length: null
  },

  onLoad: function () {
    that = this;
    var username = app.globalData.username;
    that.setData({
      username: username
    });
    // 定时服务器获取订单信息
    setTimeout(function () {
      that.getDealsInfo();
    },500);
    setInterval(function () {
        that.getDealsInfo();
    }, 10200);
  },

  // wx.request远程服务器获取报警信息
  getDealsInfo: function () {
    var that = this; // 避免成为wx.request的this冲突
    wx.request({
      url: 'https://car.shiliangproject.cn/deal/showdeals',
      data: {
        username: that.data.username
      },
      // 请求头
      header: {
        "Content-Type": 'application/json'
      },
      method: "GET",
      success: function(res) {
        var resLength = res['data'].length;
        console.log(res['data']);
        that.setData({
          deals: res['data'],
          length: resLength
        });
      }
    })
  },

  // 订单确认处理
  confirmdeal: function(event) {
    var that = this;
    var username = that.data.username;
    var equip_id = event.currentTarget.dataset.equipid;
    var id = event.currentTarget.dataset.id;
    var licence_plate = event.currentTarget.dataset.licence;
    var start_time = event.currentTarget.dataset.start;
    wx.request({
      url: 'https://car.shiliangproject.cn/deal/confirmdeal',
      data: {
        username: username,
        id: id,
        equip_id: equip_id,
        licence_plate: licence_plate,
        start_time: start_time
      },
      // 请求头
      header: {
        "Content-Type": 'application/json'
      },
      method: "GET",
      success: function (res) {
        console.log(res['data']);
        if (res['data']['res_deal'] == 1 && res['data']['res_equip'] == 1 && res['data']['res_car'] == 1) {
          console.log('success');
          common.showTip("确认订单成功");
          that.onLoad();
        }
      }
    });
  },

  // 订单删除操作
  deldeal: function(event) {
    var id = event.currentTarget.dataset.id;
    wx.request({
      url: 'https://car.shiliangproject.cn/deal/deldeal',
      data: {
        id: id,
      },
      // 请求头
      header: {
        "Content-Type": 'application/json'
      },
      method: "GET",
      success: function (res) {
        console.log(res['data']);
        if (res['data']['res'] == 1) {
          console.log('success');
          common.showTip("删除订单成功");
          that.onLoad();
        }
      }
    });
  }



})



